package com.edu.service.impl;

import com.edu.bean.Book;
import com.edu.mapper.BookMapper;
import com.edu.service.BookService;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author 75099637@qq.com
 * @PackageName SpringMybatis
 * @Package com.springMybatis.service.impl
 * @Date 2023/11/3 8:52
 * @Version 1.0
 */
@Service
public class BookServiceImpl implements BookService {
    @Resource
    private BookMapper bookMapper;
    @Override
    public int addBook(Book book) {
        if (bookMapper.selectByBookName(book.getBookName()) != null){
            return -1;
        }
        return bookMapper.insert(book);
    }

    @Override
    public int delBook(int bookId) {
        return bookMapper.deleteByPrimaryKey(bookId);
    }

    @Override
    public List<Book> getBook(Book book) {
        return bookMapper.selectBookCondition(book);
    }

    @Override
    public List<Book> getAllBook() {
        return bookMapper.selectAll();
    }

    @Override
    public int updateBook(Book book) {
        return bookMapper.updateByPrimaryKey(book);
    }

    @Override
    public Book getBookById(int bookId) {
        return bookMapper.selectByPrimaryKey(bookId);
    }
    @Override
    public List<Book> getBookByPage(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return bookMapper.selectAll();
    }

    @Override
    public int getPageCount(int pageSize) {
        int total = bookMapper.selectCountBook();
        return total / pageSize;
    }

}
